package com.zl.tby.service.impl;

import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.model.TbyAnimalPlant;
import com.zl.tby.model.TbySummarizeData;
import com.zl.tby.repository.TbyAnimalPlantRepo;
import com.zl.tby.repository.TbySummarizeDataRepo;
import com.zl.tby.service.ITbyAnimalPlantService;
import com.zl.tby.service.ITbySummarizeDataService;
import com.zl.tby.vo.TbyAnimalPlantUseOverviewBean;
import com.zl.tby.vo.TbySummarizeDataBean;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 山峰信息接口实现类
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbySummarizeDataServiceImpl implements ITbySummarizeDataService {

    @Db
    private TbySummarizeDataRepo tbySummarizeDataRepo;
    @Db
    private TbyAnimalPlantRepo tbyAnimalPlantRepo;

    @Override
    @Tran
    public R<TbySummarizeData> add(TbySummarizeData entity) {
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbySummarizeDataRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbySummarizeData> update(TbySummarizeData entity) {
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbySummarizeDataRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public TbySummarizeData getDetail() {
        List<TbySummarizeData> dataList = tbySummarizeDataRepo.selectList(mp -> { mp.whereTrue(); });
        if (dataList == null || dataList.size() == 0) {
            return new TbySummarizeData();
        }
        return dataList.get(0);
    }

    @Override
    public TbySummarizeDataBean getDetailFull() {
        TbySummarizeData entity = getDetail();
        if (entity == null) {
            return null;
        }
        TbyAnimalPlantUseOverviewBean one = null;
        TbyAnimalPlantUseOverviewBean two = null;
        TbyAnimalPlantUseOverviewBean three = null;
        if (entity.getSpeciesOneId() != null) {
            one = this.tbyAnimalPlantRepo.getAnimalPlantAndCategoryLayer(entity.getSpeciesOneId());
        }
        if (entity.getSpeciesTwoId() != null) {
            two = this.tbyAnimalPlantRepo.getAnimalPlantAndCategoryLayer(entity.getSpeciesTwoId());
        }
        if (entity.getSpeciesThreeId() != null) {
            three = this.tbyAnimalPlantRepo.getAnimalPlantAndCategoryLayer(entity.getSpeciesThreeId());
        }
        return TbySummarizeDataBean.convertByEntity(entity, one, two, three);
    }

    @Override
    public boolean summarizeDataUse(Long animalPlantId) {
        TbySummarizeData entity = getDetail();
        if (entity == null) {
            return true;
        }
        if (entity.getSpeciesOneId() != null && entity.getSpeciesOneId().compareTo(animalPlantId) == 0) {
            return false;
        } if (entity.getSpeciesTwoId() != null && entity.getSpeciesTwoId().compareTo(animalPlantId) == 0) {
            return false;
        } if (entity.getSpeciesThreeId() != null && entity.getSpeciesThreeId().compareTo(animalPlantId) == 0) {
            return false;
        }
        return true;
    }
}
